/*
 
 dukey
 
 */

namespace DukeyGetColor
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Windows.Forms;

    public class MainFrm : Form
    {
        private BackgroundWorker BW_Main;
        private IContainer components;
        private ContextMenuStrip contextMenuStrip1;
        private NumericUpDown dropBlue;
        private NumericUpDown dropGreen;
        private NumericUpDown dropRed;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private int m_CaptureX;
        private int m_CaptureY;
        private AdobeColors.CMYK m_cmyk;
        private Label m_lbl_Cyan;
        private Label m_lbl_Cyan_Symbol;
        private Label m_lbl_K;
        private Label m_lbl_Magenta;
        private Label m_lbl_Magenta_Symbol;
        private Label m_lbl_Yellow;
        private Label m_lbl_Yellow_Symbol;
        private object m_LockObj = new object();
        private Bitmap m_ScreenCapture = new Bitmap(50, 50);
        private TextBox m_txt_Cyan;
        private TextBox m_txt_K;
        private TextBox m_txt_Magenta;
        private TextBox m_txt_Yellow;
        private NotifyIcon notify1;
        private PictureBox pbArea;
        private PictureBox pbZoom;
        private System.Windows.Forms.Timer timer1;
        private ToolStripMenuItem tlExit;
        private TextBox txtColorValue;

        public MainFrm()
        {
            this.InitializeComponent();
        }

        [DllImport("gdi32.dll")]
        private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
        private void BW_Main_DoWork(object sender, DoWorkEventArgs e)
        {
            while (!this.BW_Main.CancellationPending)
            {
                this.CreateScreenCapture();
                Thread.Sleep(40);
            }
        }

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData);
        private void CreateScreenCapture()
        {
            lock (this.m_LockObj)
            {
                using (Graphics graphics = Graphics.FromImage(this.m_ScreenCapture))
                {
                    this.m_CaptureX = Control.MousePosition.X - 50;
                    this.m_CaptureY = Control.MousePosition.Y - 50;
                    graphics.CopyFromScreen(this.m_CaptureX, this.m_CaptureY, -25, -25, new Size(100, 100));
                    this.pbZoom.BackgroundImage = this.m_ScreenCapture.GetThumbnailImage(100, 100, null, IntPtr.Zero);
                }
            }
            if (base.InvokeRequired)
            {
                VoidCallback method = new VoidCallback(this.Invalidate);
                base.Invoke(method, null);
            }
            else
            {
                base.Invalidate();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void GetColor()
        {
            Graphics g = Graphics.FromHdc(CreateDC("DISPLAY", null, null, IntPtr.Zero));
            Bitmap image = new Bitmap(1, 1, g);
            Graphics graphics2 = Graphics.FromImage(image);
            IntPtr hdc = g.GetHdc();
            IntPtr hdcDest = graphics2.GetHdc();
            BitBlt(hdcDest, 0, 0, 1, 1, hdc, Control.MousePosition.X, Control.MousePosition.Y, 0xcc0020);
            g.ReleaseHdc(hdc);
            graphics2.ReleaseHdc(hdcDest);
            this.pbArea.BackColor = image.GetPixel(0, 0);
            this.txtColorValue.Text = ColorTranslator.ToHtml(this.pbArea.BackColor);
            this.dropGreen.Text = this.pbArea.BackColor.G.ToString();
            this.dropRed.Text = this.pbArea.BackColor.R.ToString();
            this.dropBlue.Text = this.pbArea.BackColor.B.ToString();
            g.Dispose();
            graphics2.Dispose();
            image.Dispose();
            this.m_cmyk = AdobeColors.RGB_to_CMYK(this.pbArea.BackColor);
            this.m_txt_Cyan.Text = this.Round(this.m_cmyk.C * 100.0).ToString();
            this.m_txt_Magenta.Text = this.Round(this.m_cmyk.M * 100.0).ToString();
            this.m_txt_Yellow.Text = this.Round(this.m_cmyk.Y * 100.0).ToString();
            this.m_txt_K.Text = this.Round(this.m_cmyk.K * 100.0).ToString();
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label1 = new System.Windows.Forms.Label();
            this.txtColorValue = new System.Windows.Forms.TextBox();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.dropRed = new System.Windows.Forms.NumericUpDown();
            this.dropGreen = new System.Windows.Forms.NumericUpDown();
            this.dropBlue = new System.Windows.Forms.NumericUpDown();
            this.BW_Main = new System.ComponentModel.BackgroundWorker();
            this.notify1 = new System.Windows.Forms.NotifyIcon(this.components);
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.tlExit = new System.Windows.Forms.ToolStripMenuItem();
            this.m_lbl_Yellow_Symbol = new System.Windows.Forms.Label();
            this.m_lbl_Magenta_Symbol = new System.Windows.Forms.Label();
            this.m_lbl_Cyan_Symbol = new System.Windows.Forms.Label();
            this.m_lbl_K = new System.Windows.Forms.Label();
            this.m_lbl_Yellow = new System.Windows.Forms.Label();
            this.m_lbl_Magenta = new System.Windows.Forms.Label();
            this.m_lbl_Cyan = new System.Windows.Forms.Label();
            this.m_txt_K = new System.Windows.Forms.TextBox();
            this.m_txt_Yellow = new System.Windows.Forms.TextBox();
            this.m_txt_Magenta = new System.Windows.Forms.TextBox();
            this.m_txt_Cyan = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.pbZoom = new System.Windows.Forms.PictureBox();
            this.pbArea = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.dropRed)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dropGreen)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dropBlue)).BeginInit();
            this.contextMenuStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pbZoom)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbArea)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(185, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "颜色值";
            // 
            // txtColorValue
            // 
            this.txtColorValue.Location = new System.Drawing.Point(232, 7);
            this.txtColorValue.Name = "txtColorValue";
            this.txtColorValue.Size = new System.Drawing.Size(50, 21);
            this.txtColorValue.TabIndex = 1;
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(185, 44);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(53, 12);
            this.label2.TabIndex = 0;
            this.label2.Text = "红色值：";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(185, 75);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(53, 12);
            this.label3.TabIndex = 0;
            this.label3.Text = "绿色值：";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(185, 107);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(53, 12);
            this.label4.TabIndex = 0;
            this.label4.Text = "蓝色值：";
            // 
            // dropRed
            // 
            this.dropRed.Location = new System.Drawing.Point(232, 34);
            this.dropRed.Maximum = new decimal(new int[] {
            255,
            0,
            0,
            0});
            this.dropRed.Name = "dropRed";
            this.dropRed.Size = new System.Drawing.Size(50, 21);
            this.dropRed.TabIndex = 4;
            // 
            // dropGreen
            // 
            this.dropGreen.Location = new System.Drawing.Point(232, 66);
            this.dropGreen.Maximum = new decimal(new int[] {
            255,
            0,
            0,
            0});
            this.dropGreen.Name = "dropGreen";
            this.dropGreen.Size = new System.Drawing.Size(50, 21);
            this.dropGreen.TabIndex = 4;
            // 
            // dropBlue
            // 
            this.dropBlue.Location = new System.Drawing.Point(232, 98);
            this.dropBlue.Maximum = new decimal(new int[] {
            255,
            0,
            0,
            0});
            this.dropBlue.Name = "dropBlue";
            this.dropBlue.Size = new System.Drawing.Size(50, 21);
            this.dropBlue.TabIndex = 4;
            // 
            // BW_Main
            // 
            this.BW_Main.WorkerSupportsCancellation = true;
            this.BW_Main.DoWork += new System.ComponentModel.DoWorkEventHandler(this.BW_Main_DoWork);
            // 
            // notify1
            // 
            this.notify1.ContextMenuStrip = this.contextMenuStrip1;
            this.notify1.Visible = true;
            this.notify1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notify1_MouseDoubleClick);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tlExit});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(101, 26);
            // 
            // tlExit
            // 
            this.tlExit.Name = "tlExit";
            this.tlExit.Size = new System.Drawing.Size(100, 22);
            this.tlExit.Text = "退出";
            this.tlExit.Click += new System.EventHandler(this.tlExit_Click);
            // 
            // m_lbl_Yellow_Symbol
            // 
            this.m_lbl_Yellow_Symbol.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.m_lbl_Yellow_Symbol.Location = new System.Drawing.Point(351, 64);
            this.m_lbl_Yellow_Symbol.Name = "m_lbl_Yellow_Symbol";
            this.m_lbl_Yellow_Symbol.Size = new System.Drawing.Size(19, 22);
            this.m_lbl_Yellow_Symbol.TabIndex = 57;
            this.m_lbl_Yellow_Symbol.Text = "%";
            // 
            // m_lbl_Magenta_Symbol
            // 
            this.m_lbl_Magenta_Symbol.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.m_lbl_Magenta_Symbol.Location = new System.Drawing.Point(351, 37);
            this.m_lbl_Magenta_Symbol.Name = "m_lbl_Magenta_Symbol";
            this.m_lbl_Magenta_Symbol.Size = new System.Drawing.Size(19, 23);
            this.m_lbl_Magenta_Symbol.TabIndex = 56;
            this.m_lbl_Magenta_Symbol.Text = "%";
            // 
            // m_lbl_Cyan_Symbol
            // 
            this.m_lbl_Cyan_Symbol.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.m_lbl_Cyan_Symbol.Location = new System.Drawing.Point(351, 10);
            this.m_lbl_Cyan_Symbol.Name = "m_lbl_Cyan_Symbol";
            this.m_lbl_Cyan_Symbol.Size = new System.Drawing.Size(19, 23);
            this.m_lbl_Cyan_Symbol.TabIndex = 55;
            this.m_lbl_Cyan_Symbol.Text = "%";
            // 
            // m_lbl_K
            // 
            this.m_lbl_K.Location = new System.Drawing.Point(288, 93);
            this.m_lbl_K.Name = "m_lbl_K";
            this.m_lbl_K.Size = new System.Drawing.Size(19, 17);
            this.m_lbl_K.TabIndex = 54;
            this.m_lbl_K.Text = "K:";
            // 
            // m_lbl_Yellow
            // 
            this.m_lbl_Yellow.Location = new System.Drawing.Point(288, 67);
            this.m_lbl_Yellow.Name = "m_lbl_Yellow";
            this.m_lbl_Yellow.Size = new System.Drawing.Size(19, 17);
            this.m_lbl_Yellow.TabIndex = 53;
            this.m_lbl_Yellow.Text = "Y:";
            // 
            // m_lbl_Magenta
            // 
            this.m_lbl_Magenta.Location = new System.Drawing.Point(288, 41);
            this.m_lbl_Magenta.Name = "m_lbl_Magenta";
            this.m_lbl_Magenta.Size = new System.Drawing.Size(19, 17);
            this.m_lbl_Magenta.TabIndex = 52;
            this.m_lbl_Magenta.Text = "M:";
            // 
            // m_lbl_Cyan
            // 
            this.m_lbl_Cyan.Location = new System.Drawing.Point(288, 15);
            this.m_lbl_Cyan.Name = "m_lbl_Cyan";
            this.m_lbl_Cyan.Size = new System.Drawing.Size(19, 18);
            this.m_lbl_Cyan.TabIndex = 51;
            this.m_lbl_Cyan.Text = "C:";
            // 
            // m_txt_K
            // 
            this.m_txt_K.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.m_txt_K.Location = new System.Drawing.Point(308, 91);
            this.m_txt_K.Name = "m_txt_K";
            this.m_txt_K.Size = new System.Drawing.Size(42, 21);
            this.m_txt_K.TabIndex = 50;
            // 
            // m_txt_Yellow
            // 
            this.m_txt_Yellow.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.m_txt_Yellow.Location = new System.Drawing.Point(308, 64);
            this.m_txt_Yellow.Name = "m_txt_Yellow";
            this.m_txt_Yellow.Size = new System.Drawing.Size(42, 21);
            this.m_txt_Yellow.TabIndex = 49;
            // 
            // m_txt_Magenta
            // 
            this.m_txt_Magenta.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.m_txt_Magenta.Location = new System.Drawing.Point(308, 37);
            this.m_txt_Magenta.Name = "m_txt_Magenta";
            this.m_txt_Magenta.Size = new System.Drawing.Size(42, 21);
            this.m_txt_Magenta.TabIndex = 48;
            // 
            // m_txt_Cyan
            // 
            this.m_txt_Cyan.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.m_txt_Cyan.Location = new System.Drawing.Point(308, 10);
            this.m_txt_Cyan.Name = "m_txt_Cyan";
            this.m_txt_Cyan.Size = new System.Drawing.Size(42, 21);
            this.m_txt_Cyan.TabIndex = 47;
            // 
            // label5
            // 
            this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label5.Location = new System.Drawing.Point(351, 94);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(19, 22);
            this.label5.TabIndex = 58;
            this.label5.Text = "%";
            // 
            // pbZoom
            // 
            this.pbZoom.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pbZoom.Cursor = System.Windows.Forms.Cursors.Cross;
            this.pbZoom.Location = new System.Drawing.Point(68, 12);
            this.pbZoom.Name = "pbZoom";
            this.pbZoom.Size = new System.Drawing.Size(100, 100);
            this.pbZoom.TabIndex = 5;
            this.pbZoom.TabStop = false;
            this.pbZoom.Click += new System.EventHandler(this.pbZoom_Click);
            // 
            // pbArea
            // 
            this.pbArea.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pbArea.Cursor = System.Windows.Forms.Cursors.Cross;
            this.pbArea.Location = new System.Drawing.Point(3, 12);
            this.pbArea.Name = "pbArea";
            this.pbArea.Size = new System.Drawing.Size(60, 60);
            this.pbArea.TabIndex = 2;
            this.pbArea.TabStop = false;
            this.pbArea.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pbArea_MouseDown);
            this.pbArea.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pbArea_MouseUp);
            // 
            // MainFrm
            // 
            this.ClientSize = new System.Drawing.Size(374, 137);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.m_lbl_Yellow_Symbol);
            this.Controls.Add(this.m_lbl_Magenta_Symbol);
            this.Controls.Add(this.m_lbl_Cyan_Symbol);
            this.Controls.Add(this.m_lbl_K);
            this.Controls.Add(this.m_lbl_Yellow);
            this.Controls.Add(this.m_lbl_Magenta);
            this.Controls.Add(this.m_lbl_Cyan);
            this.Controls.Add(this.m_txt_K);
            this.Controls.Add(this.m_txt_Yellow);
            this.Controls.Add(this.m_txt_Magenta);
            this.Controls.Add(this.m_txt_Cyan);
            this.Controls.Add(this.pbZoom);
            this.Controls.Add(this.dropBlue);
            this.Controls.Add(this.dropGreen);
            this.Controls.Add(this.dropRed);
            this.Controls.Add(this.txtColorValue);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.pbArea);
            this.MaximizeBox = false;
            this.Name = "MainFrm";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Dukey拾色器";
            this.TopMost = true;
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MainFrm_FormClosed);
            this.Resize += new System.EventHandler(this.MainFrm_Resize);
            ((System.ComponentModel.ISupportInitialize)(this.dropRed)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dropGreen)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dropBlue)).EndInit();
            this.contextMenuStrip1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pbZoom)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbArea)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void linkLabel1_Click(object sender, EventArgs e)
        {
            Process process = new Process();
            process.StartInfo.FileName = "iexplore.exe";
            process.StartInfo.Arguments = "http://hi.baidu.com/38809972";
            process.Start();
        }

        private void MainFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.notify1.Dispose();
            Environment.Exit(0);
        }

        private void MainFrm_Resize(object sender, EventArgs e)
        {
            if (base.WindowState == FormWindowState.Minimized)
            {
                base.Hide();
            }
        }

        private void notify1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (!base.Visible)
            {
                base.Show();
                base.WindowState = FormWindowState.Normal;
            }
            else
            {
                base.Hide();
            }
        }

        private void pbArea_MouseDown(object sender, MouseEventArgs e)
        {
            this.pbArea.Cursor = Cursors.Cross;
            this.timer1.Enabled = true;
            if (!this.BW_Main.IsBusy)
            {
                this.BW_Main.RunWorkerAsync();
            }
        }

        private void pbArea_MouseUp(object sender, MouseEventArgs e)
        {
            this.timer1.Enabled = false;
            this.txtColorValue.SelectAll();
            this.txtColorValue.Copy();
            this.BW_Main.CancelAsync();
        }

        private void pbZoom_Click(object sender, EventArgs e)
        {
            this.GetColor();
        }

        private int Round(double val)
        {
            int num = (int) val;
            int num2 = (int) (val * 100.0);
            if ((num2 % 100) >= 50)
            {
                num++;
            }
            return num;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.GetColor();
        }

        private void tlExit_Click(object sender, EventArgs e)
        {
            this.notify1.Dispose();
            Environment.Exit(0);
        }
    }
}

